package com.jebel.framwork.common.util;

import com.jebel.framwork.common.response.RespBodyBuilder;
import com.jebel.framwork.common.response.RespCode;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CompletableFuture;

public class VertxSendFutureUtil {
    private static Logger logger = LoggerFactory.getLogger(VertxSendFutureUtil.class);

    public static void vertxSendFuture(Vertx vertx, String serviceAddress, JsonObject serviceParam, DeliveryOptions deliveryOptions, CompletableFuture<String> future) {
        vertx.eventBus().request(serviceAddress, serviceParam, deliveryOptions, messageAsyncResult -> {
            if (messageAsyncResult.succeeded()) {
                String result = messageAsyncResult.result().body().toString();
                future.complete(result);
            } else {
                logger.error("address:{}:param:{}:error:{}", serviceAddress, serviceParam, messageAsyncResult.cause());
                future.complete(RespBodyBuilder.toError(RespCode.CODE_10600));
            }
        });
    }
}
